<!--
  供应商管理
 -->

<template>
  <BaseMainSlot>
    <template v-slot:btn-box>
      <ActionBar
        :page-no="76"
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        :has-edit="false"
        :has-save="false"
        :has-cancel="false"
        @add="add"
        @cancel="cancel('formData', 'tableData')"
        @edit="edit"
        @del="del"
        @save="save('formData')"
      />
    </template>
    <template v-slot:search-box>
      <SearchForm :formConfig="formConfig" :formData="searchlist" :labelWidth="'125px'" @search="search"></SearchForm>
    </template>
    <template v-slot:table-box>
      <Table @sortChange="changeTableSort" :propList="tableColumn" v-loading="loading"
        :listData="tableData" @handleSelectionChange="handleSelectionChange">
        <template v-slot:operation="{ row }">
          <template v-if="!row.index">
            <el-button
              v-if="judgePermission(76, 'edit')"
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="handleEdit(row)"
            >
              <i class="el-icon-edit-outline"></i> 修改
            </el-button>
            <el-button
              v-if="judgePermission(76, 'delete')"
              class="text-btn-cursor cursor-delete"
              type="text"
              size="small"
              @click="handleDelete(row)"
            >
              <i class="el-icon-delete"></i> 删除
            </el-button>
          </template>
        </template>
        <template v-slot:status="{ row, item }">
          <template v-if="!row.index">
            {{ row[item.prop] === true ? '是' : '否' }}
          </template>
        </template>
      </Table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="dialogTitle + '供应商信息'"
        :visible.sync="dialogFormVisible"
      >
        <Form
          ref="formData"
          :labelWidth="'150px'"
          :formData="formData"
          :formConfig="formlist"
          :rules="rules"
        >
        </Form>
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">取 消</el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >保 存</el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import SearchForm from '@/components/searchForm';
import Form from '@/components/searchForm/formlist';
import Table from '@/components/table/table.vue';
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot";
import supplierApi from "@/api/livestock-manage/purchaseManage/supplier";
import supplierTypeApi from "@/api/livestock-manage/purchaseManage/supplierType";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

export default {
  name: "SupplierManage",
  components: {
    SearchForm,
    Form,
    Table,
    ActionBar,
    Pagination,
    BaseMainSlot,
  },
  mixins: [mymixin],
  data() {
    return {
      loading: false,
      tableColumn: [],
      searchlist: {},
      formConfig: [
        {
          label: '供应商名称',
          prop: 'supplierName',
          type: 'input',
        },
        {
          label: '供应商简称',
          prop: 'shortName',
          type: 'input',
        },
        {
          label: '供应商类型',
          prop: 'supplierTypeNo',
          type: 'select',
          options: [],
          optionKey: { label: 'label', value: 'value' },
        },
        {
          label: "统一社会信用代码",
          prop: "creditCode",
          type: "input",
        },
        {
          label: "所属行业",
          prop: "trade",
          type: "input",
        },
      ],
      formlist: [
        {
          label: '供应商名称',
          prop: 'supplierName',
          type: 'input',
          required: true
        },
        {
          label: '供应商简称',
          prop: 'shortName',
          type: 'input',
        },
        {
          label: '供应商类型',
          prop: 'supplierTypeNo',
          type: 'select',
          options: [],
          // required: true
        },
        {
          label: "统一社会信用代码",
          prop: "creditCode",
          type: "input",
          // required: true
        },
        {
          label: "所属行业",
          prop: "trade",
          type: "input",
          // required: true
        },
        {
          label: "散户",
          prop: "isFreecust",
          type: "select",
          options: [
            { label: "是", value: true },
            { label: "否", value: false }
          ],
          optionKey: { label: 'label', value: 'value' },
          required: true
        },
        {
          label: "法人",
          prop: "legalBody",
          type: "input",
          // required: true
        },
        {
          type: "input",
          prop: "address",
          label: "企业地址",
        },
        {
          type: "input",
          prop: "tel",
          label: "电话",
          // required: true
        },
        {
          type: "input",
          prop: "url",
          label: "Web网址",
        },
        {
          type: "input",
          prop: "email",
          label: "e-mail地址",
        },
        {
          type: "input",
          prop: "country",
          label: "国家/地区",
          // required: true
        },
        {
          type: "input",
          prop: "bank",
          label: "开户行",
          // required: true
        },
        {
          type: "input",
          prop: "bankAccount",
          label: "银行帐号",
          // required: true
        },
        {
          type: "select",
          prop: "enableStatus",
          label: "是否启用",
          options: [
            { label: "是", value: true },
            { label: "否", value: false }
          ],
          optionKey: { label: 'label', value: 'value' },
          required: true
        },
        {
          type: "input",
          prop: "remarks",
          label: "备注",
        }
      ],
      rules: {},
      formData: {},
      dialogTitle: "",
      dialogFormVisible: false,
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    }),
  },
  watch: {
    dialogFormVisible(v) {
      if (v) {
        this.$nextTick(()=>{
          this.$refs.formData.resetForm()
        })
      }
    }
  },
  created() {
    let list = []
    this.formlist.map(el => {
      let obj = {
        prop: el.prop, label: el.label, minWidth: '140px'
      }
      if (obj.label === '供应商类型') obj.prop = 'supplierTypeName'
      if (['散户', '是否启用'].includes(el.label)) obj.slotName = 'status'
      list.push(obj)
    })
    list.push({ prop: 'operation', label: '操作', slotName: 'operation', width: '120' })
    this.tableColumn = list
    this.getTableDataList();
    this.getSupplierTypeList();
  },
  mounted() {
    
  },
  methods: {
    getSupplierTypeList() {
      supplierTypeApi.conditionQuery({}).then(res => {
        console.log(res)
        if (res.code === 1) {
          let list = [];
          // 提供表头搜索数据
          res.data.forEach(item => {
            list.push({
              label: item.supplierTypeName,
              value: item.supplierTypeNo
            });
          });
          this.setOptions(this.formConfig, 'supplierTypeNo', list)
          this.setOptions(this.formlist, 'supplierTypeNo', list)
        }
      });
    },
    // 查询表格数据
    getTableDataList() {
      let param = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          ...this.searchlist
        }
      };
      supplierApi.pageQuery(param).then(res => {
        this.loading = false;
        let data = res.data;
        if (data) {
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    add() {
      this.formData = {};
      this.canInput = true;
      this.openDialog("add");
    },
    handleEdit(row) {
      this.formData = { ...row };
      this.editActive = true;
      this.saveActive = false;
      this.cancelActive = true;
      this.canInput = false;
      this.openDialog("edit");
    },
    handleSelectionChange(list) {
      this.supplierIdList = [];
      this.supplierIdList = list.map(item => item.supplierNo);
      if (this.supplierIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      supplierApi.numberBatchDel({
        noList: this.supplierIdList
      }).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.getTableDataList();
        } else {
          this.$message.warning(res.msg || "删除失败");
        }
      });
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          supplierApi.numberBatchDel({
            noList: [row.supplierNo]
          }).then(res => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.getTableDataList();
            } else {
              this.$message.warning(res.msg || "删除失败");
            }
          });
        })
        .catch(() => {
          this.$message("取消删除");
        });
    },
    save(formData) {
      let ruleForm = this.$refs[formData]?.$refs.ruleForm
      ruleForm?.validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            supplierApi.edit(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "修改成功！");
                ruleForm.resetFields();
                this.closeDialog();
                this.getTableDataList();
              } else {
                this.$message.error(res.msg || "修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            supplierApi.add(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "新增成功！");
                ruleForm.resetFields();
                this.closeDialog();
                this.getTableDataList();
              } else {
                this.$message.error(res.msg || "新增失败！");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = "";
    },
  }
};
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";

.addInfo {
  width: 100%;
}
::v-deep .el-form-item__label {
  min-width: 120px !important;
}
.search-item {
  display: flex;
  height: 36px;
  margin-left: 20px;
  white-space: nowrap;
  &:first-child {
    margin: 0;
  }
  span {
    display: inline-block;
  }
  &.rules-wrap span {
    padding: 0 15px;
    border: 1px solid $borderColor;
    cursor: pointer;
    transition: border-color 0.2s;
    box-sizing: border-box;
    &:hover {
      border-color: #6ecae3;
    }
  }
}

.el-date-editor,
.el-select {
  width: 100%;
}

h4.total-price {
  display: flex;
  justify-content: space-between;
  margin: 0;
}

.right-table {
  line-height: 0px;
}
.adress {
  ::v-deep .el-form-item__content {
    flex-wrap: wrap;
    .el-cascader {
      font-size: 10px;
      // height: 20px;
      // line-height: 22px;
      width: 100%;
      .el-input__inner {
        font-size: 10px;
        height: 30px;
      }
    }
  }
}

// 上传图片
::v-deep .el-upload--picture-card {
  width: 40px;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.1);
  border: 1px solid #6ecae3;
}
::v-deep .el-upload--picture-card {
  position: relative;
}
::v-deep .el-upload--picture-card .el-icon-plus {
  font-size: 34px;
  color: #6ecae3;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>
